<?php

namespace App\Http\Controllers\Admin;

use App\Models\Goods;
use App\Models\Shop;
use App\Services\Service\Helper;
use Illuminate\Http\Request;

class GoodsController extends BaseController
{
    //
    public function __construct()
    {
        $shops = Shop::where(['status' => 1])->get();
        $this->assign('shops', $shops);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $inputs = Helper::getInputs($request, ['opt_type' => '', 'keywords' => '', 'page' => 1, 'limit' => 10, 'status' => '', 'is_hot' => '']);
        $page = intval($inputs['page']);
        $limit = intval($inputs['limit']);
        $start = ($page - 1) * $limit;

        $where = [];
        $where['where'][] = [
            'is_delete' => 0
        ];
        if ($inputs['opt_type'] != '' && $inputs['keywords'] != '') {
            if (in_array($inputs['opt_type'], ['id', 'item_id'])) {
                $where['where'][] = [$inputs['opt_type'] => $inputs['keywords']];
            } else if (in_array($inputs['opt_type'], ['sid', 'title'])) {
                $where['whereHas'][] = ['shop', function ($query) use ($inputs) {
                    $query->where($inputs['opt_type'], 'like', '%' . $inputs['keywords'] . '%');
                }];
            } else {
                $where['where'][] = [$inputs['opt_type'], 'like', '%' . $inputs['keywords'] . '%'];
            }
        }
        if ($inputs['status'] != '') {
            $where['where'][] = ['status' => $inputs['status']];
        }
        $result = Goods::queryAll($where, [], [$start, $limit]);
        $result['conditions'] = $inputs;
        return $this->display('goods.index', $result);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        return $this->display('goods.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        if ($request->ajax()) {
            $item_id = $request->input('item_id', '');
            if ($item_id == '') {
                return $this->error('请填写宝贝ID');
            }
            if (Goods::isExist($item_id)) {
                return $this->error('已经存在ID:' . $item_id . '的宝贝信息');
            }
            $item_title = $request->input('item_title', '');
            if ($item_title == '') {
                return $this->error('请填写宝贝标题');
            }
            $shop_id = $request->input('shop_id', 0);
            if (!$shop_id) {
                return $this->error('请选择所属店铺');
            }
            $price = $request->input('price', 0);
            if ($price <= 0) {
                return $this->error('请填写宝贝价格');
            }
            $commission = $request->input('commission', 0);
            if ($commission <= 0) {
                return $this->error('请填写宝贝佣金');
            }
            $goods = new Goods();
            $goods->shop_id = $shop_id;
            $goods->item_title = $item_title;
            $goods->item_id = $item_id;
            $goods->price = $price;
            $goods->commission = $commission;
            if (false !== $goods->save()) {
                return $this->success('新增成功', ['redirect' => url('goods')]);
            } else {
                return $this->error('新增失败');
            }
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        return $this->display('goods.show');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $goods = Goods::find($id);
        $this->assign('vo', $goods);
        return $this->display('goods.edit');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if ($request->ajax()) {
            $goods = Goods::find($id);
            if (is_null($goods)) {
                return $this->error('查找宝贝信息失败');
            }
            $item_id = $request->input('item_id', '');
            if ($item_id == '') {
                return $this->error('请填写宝贝ID');
            }
            $item_title = $request->input('item_title', '');
            if ($item_title == '') {
                return $this->error('请填写宝贝标题');
            }
            if (Goods::isExist($item_id, $id)) {
                return $this->error('已经存在ID:' . $item_id . '的宝贝信息');
            }
            $shop_id = $request->input('shop_id', 0);
            if (!$shop_id) {
                return $this->error('请选择所属店铺');
            }
            $price = $request->input('price', 0);
            if ($price <= 0) {
                return $this->error('请填写宝贝价格');
            }
            $commission = $request->input('commission', 0);
            if ($commission <= 0) {
                return $this->error('请填写宝贝佣金');
            }
            //
            $goods->shop_id = $shop_id;
            $goods->item_title = $item_title;
            $goods->item_id = $item_id;
            $goods->price = $price;
            $goods->commission = $commission;
            if (false !== $goods->save()) {
                return $this->success('更新成功', ['redirect' => url('goods')]);
            } else {
                return $this->error('更新失败');
            }
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request, $id)
    {
        if ($request->ajax()) {
            $goods = Goods::find($id);
            if (is_null($goods)) {
                return $this->error('查找宝贝信息失败');
            }
            $goods->is_delete = 1;
            if (false !== $goods->save()) {
                return $this->success('删除成功');
            } else {
                return $this->error('删除失败');
            }
        }
    }

    /**
     * 宝贝状态调整
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function switch_status(Request $request)
    {
        if ($request->ajax()) {
            $id = $request->input('id', 0);
            $value = $request->input('value', 2);
            if ($value == 2) {
                $value = 1;
            } else {
                $value = 2;
            }
            $goods = Goods::find($id);
            if (is_null($goods)) {
                return $this->error('宝贝信息不存在');
            } else {
                $goods->status = $value;
                $goods->save();
                return $this->success('success', ['value' => $value]);
            }
        }
    }

    /**
     * 根据商家ID获取对应的商品ID
     *
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function goods_list(Request $request)
    {
        if ($request->ajax()) {
            $id = $request->input('id',0);
            $goods_list = Goods::getByShopId($id);
            $result = [];
            foreach ($goods_list as $goods) {
                $result[$goods->id] = [
                    'id' => $goods->id,
                    'item_id' => $goods->item_id,
                    'item_title' => $goods->item_title,
                    'price' => $goods->price,
                    'commission' => $goods->commission
                ];
            }
            return $this->success('success', $result);
        }
    }
}
